using System;
using System.Collections.Generic;
using System.Text;
using XProject.DAO;
namespace XProject.Entity
{
   public class Contact
    {
        int id;
        IList<Phone> phones;
        IList<Email> emails;
       PhoneDAO daoPhone = new PhoneDAO();
       EmailDAO daoEmail = new EmailDAO();
        string address;
        public Contact()
        {
            phones = new List<Phone>(); 
            emails = new List<Email>(); 
            address = "";
        }
        /// <summary>
        /// Get, set contact ID
        /// </summary>
        public int ContactID
        {
            get { return id; }
            set { id = value; }
        }
        /// <summary>
        /// Get, set list of phone number
        /// </summary>
        public IList<Phone> Phones
        {
            get { return phones; }
            set { phones = value; }
        }
        /// <summary>
        /// Get, set list of email address
        /// </summary>
        public IList<Email> Emails
        {
            get { return emails; }
            set { emails = value; }

        }
        /// <summary>
        /// Get, set address of the person
        /// </summary>
        public string Address
        {
            get { return address; }
            set { address = value; }

        }
        #region methods
        /// <summary>
        /// method to add a phone number
        /// </summary>
        /// <param name="phone"></param>
        public int AddPhoneNumber(Phone phone)
        {            
            this.Phones.Add(phone);
            int effectedRows = daoPhone.Insert(phone,this.ContactID);
            if (effectedRows <= 0) this.Phones.Remove(phone);
            return effectedRows;

        }
        /// <summary>
        /// Method to remove a phone number
        /// </summary>
        /// <param name="phone"></param>
        public int RemovePhoneNumber(Phone phone)
        {
            this.Phones.Remove(phone);
            int effectedRows = daoPhone.Delete(phone);
            if (effectedRows <= 0) this.Phones.Add(phone);
            return effectedRows;
        }
        /// <summary>
        /// method to add an email
        /// </summary>
        /// <param name="email"></param>
        public int AddEmail(Email email)
        {
            this.Emails.Add(email);
            int effectedRows = daoEmail.Insert(email,this);
            if (effectedRows <= 0) this.Emails.Remove(email);
            return effectedRows;
        }
        /// <summary>
        /// Method to remove a email from email list
        /// </summary>
        /// <param name="email"></param>
        public int RemoveEmail(Email email)
        {
            this.Emails.Remove(email);
            int effectedRows = daoEmail.Delete(email);
            if (effectedRows <= 0) this.Emails.Add(email);
            return effectedRows;
        }
       public bool ContainEmail(string email)
       {
           foreach (Email curEmail in emails)
           {
               if(curEmail.EmailAddress.ToLower().Contains(email)){
                   return true;
               }
           }
           return false;
       }
       public bool ContainPhone(string phoneNumber)
       {
           foreach (Phone phone in phones)
           {
               if (phone.PhoneNumber.ToLower().Contains(phoneNumber))
               {
                   return true;
               }
           }
           return false;
       }
        #endregion
    }
}
